﻿
namespace KMT_Pro.Web
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.ComponentModel.DataAnnotations;
    using System.Data;
    using System.Linq;
    using System.ServiceModel.DomainServices.EntityFramework;
    using System.ServiceModel.DomainServices.Hosting;
    using System.ServiceModel.DomainServices.Server;
    using System.Data.Objects;
    using KMT_Pro.Web;


    // KMT_DBEntities 컨텍스트를 사용하여 응용 프로그램 논리를 구현합니다.
    // TODO: 응용 프로그램 논리를 이러한 메서드 또는 추가 메서드에 추가합니다.
    // TODO: 인증(Windows/ASP.NET Forms)를 연결하고 다음의 주석을 제거하여 익명 액세스를 사용하지 않도록 설정합니다.
    // 또한 역할을 추가하여 액세스를 적절하게 제한합니다.
    // [RequiresAuthentication]
    [EnableClientAccess()]
    public class CustomerService : LinqToEntitiesDomainService<KMT_DBEntities>
    {

        // TODO:
        // 쿼리 메서드의 결과를 제한하십시오. 추가 입력이 필요한 경우
        // 이 메서드에 매개 변수를 추가하거나 다른 이름으로 추가 쿼리 메서드를 만들 수 있습니다.
        // 페이징을 지원하려면 'ClientAS' 쿼리에 순서를 추가해야 합니다.
        public IQueryable<ClientAS> GetClientAS()
        {
            return this.ObjectContext.ClientAS;
        }

        public void InsertClientAS(ClientAS clientAS)
        {
            if ((clientAS.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(clientAS, EntityState.Added);
            }
            else
            {
                this.ObjectContext.ClientAS.AddObject(clientAS);
            }
        }

        public void UpdateClientAS(ClientAS currentClientAS)
        {
            this.ObjectContext.ClientAS.AttachAsModified(currentClientAS, this.ChangeSet.GetOriginal(currentClientAS));
        }

        public void DeleteClientAS(ClientAS clientAS)
        {
            if ((clientAS.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(clientAS, EntityState.Deleted);
            }
            else
            {
                this.ObjectContext.ClientAS.Attach(clientAS);
                this.ObjectContext.ClientAS.DeleteObject(clientAS);
            }
        }

        // TODO:
        // 쿼리 메서드의 결과를 제한하십시오. 추가 입력이 필요한 경우
        // 이 메서드에 매개 변수를 추가하거나 다른 이름으로 추가 쿼리 메서드를 만들 수 있습니다.
        // 페이징을 지원하려면 'ClientMaster' 쿼리에 순서를 추가해야 합니다.
        public IQueryable<ClientMaster> GetClientMaster()
        {
            return this.ObjectContext.ClientMaster;
        }

        public void InsertClientMaster(ClientMaster clientMaster)
        {
            if ((clientMaster.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(clientMaster, EntityState.Added);
            }
            else
            {
                this.ObjectContext.ClientMaster.AddObject(clientMaster);
            }
        }

        public void UpdateClientMaster(ClientMaster currentClientMaster)
        {
            this.ObjectContext.ClientMaster.AttachAsModified(currentClientMaster, this.ChangeSet.GetOriginal(currentClientMaster));
        }

        public void DeleteClientMaster(ClientMaster clientMaster)
        {
            if ((clientMaster.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(clientMaster, EntityState.Deleted);
            }
            else
            {
                this.ObjectContext.ClientMaster.Attach(clientMaster);
                this.ObjectContext.ClientMaster.DeleteObject(clientMaster);
            }
        }

        // TODO:
        // 쿼리 메서드의 결과를 제한하십시오. 추가 입력이 필요한 경우
        // 이 메서드에 매개 변수를 추가하거나 다른 이름으로 추가 쿼리 메서드를 만들 수 있습니다.
        // 페이징을 지원하려면 'ClientPerson' 쿼리에 순서를 추가해야 합니다.
        public IQueryable<ClientPerson> GetClientPerson()
        {
            return this.ObjectContext.ClientPerson;
        }

        public void InsertClientPerson(ClientPerson clientPerson)
        {
            if ((clientPerson.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(clientPerson, EntityState.Added);
            }
            else
            {
                this.ObjectContext.ClientPerson.AddObject(clientPerson);
            }
        }

        public void UpdateClientPerson(ClientPerson currentClientPerson)
        {
            this.ObjectContext.ClientPerson.AttachAsModified(currentClientPerson, this.ChangeSet.GetOriginal(currentClientPerson));
        }

        public void DeleteClientPerson(ClientPerson clientPerson)
        {
            if ((clientPerson.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(clientPerson, EntityState.Deleted);
            }
            else
            {
                this.ObjectContext.ClientPerson.Attach(clientPerson);
                this.ObjectContext.ClientPerson.DeleteObject(clientPerson);
            }
        }

        // TODO:
        // 쿼리 메서드의 결과를 제한하십시오. 추가 입력이 필요한 경우
        // 이 메서드에 매개 변수를 추가하거나 다른 이름으로 추가 쿼리 메서드를 만들 수 있습니다.
        // 페이징을 지원하려면 'HardwareMaster' 쿼리에 순서를 추가해야 합니다.
        public IQueryable<HardwareMaster> GetHardwareMaster()
        {
            return this.ObjectContext.HardwareMaster;
        }

        public void InsertHardwareMaster(HardwareMaster hardwareMaster)
        {
            if ((hardwareMaster.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(hardwareMaster, EntityState.Added);
            }
            else
            {
                this.ObjectContext.HardwareMaster.AddObject(hardwareMaster);
            }
        }

        public void UpdateHardwareMaster(HardwareMaster currentHardwareMaster)
        {
            this.ObjectContext.HardwareMaster.AttachAsModified(currentHardwareMaster, this.ChangeSet.GetOriginal(currentHardwareMaster));
        }

        public void DeleteHardwareMaster(HardwareMaster hardwareMaster)
        {
            if ((hardwareMaster.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(hardwareMaster, EntityState.Deleted);
            }
            else
            {
                this.ObjectContext.HardwareMaster.Attach(hardwareMaster);
                this.ObjectContext.HardwareMaster.DeleteObject(hardwareMaster);
            }
        }

        // TODO:
        // 쿼리 메서드의 결과를 제한하십시오. 추가 입력이 필요한 경우
        // 이 메서드에 매개 변수를 추가하거나 다른 이름으로 추가 쿼리 메서드를 만들 수 있습니다.
        // 페이징을 지원하려면 'UserMaster' 쿼리에 순서를 추가해야 합니다.
        public IQueryable<UserMaster> GetUserMaster()
        {
            return this.ObjectContext.UserMaster;
        }

        public void InsertUserMaster(UserMaster userMaster)
        {
            if ((userMaster.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(userMaster, EntityState.Added);
            }
            else
            {
                this.ObjectContext.UserMaster.AddObject(userMaster);
            }
        }

        public void UpdateUserMaster(UserMaster currentUserMaster)
        {
            this.ObjectContext.UserMaster.AttachAsModified(currentUserMaster, this.ChangeSet.GetOriginal(currentUserMaster));
        }

        public void DeleteUserMaster(UserMaster userMaster)
        {
            if ((userMaster.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(userMaster, EntityState.Deleted);
            }
            else
            {
                this.ObjectContext.UserMaster.Attach(userMaster);
                this.ObjectContext.UserMaster.DeleteObject(userMaster);
            }
        }

        //--------------------프로시져 호출부분--------------------------------------//

        /// <summary>
        /// 로그인 체크 프로시져 호출 로직
        /// </summary>
        /// <param name="ID"></param>
        /// <param name="PASS"></param>
        /// <returns></returns>
        public IQueryable<SP_Login_Check_Result> SP_Login_Check(string ID, string PASS)
        {
            try
            {
                ObjectParameter Status = new ObjectParameter("iStatus", "varchar(3)");
                ObjectParameter Message = new ObjectParameter("iMessage", "varchar(255)");

                return this.ObjectContext.SP_Login_Check(ID, PASS, Status, Message).AsQueryable<SP_Login_Check_Result>();
            }
            catch (Exception)
            { return null; }
        }

        /// <summary>
        /// 업체정보 프로시져 호출 로직
        /// </summary>
        /// <returns></returns>
        public IQueryable<SP_ClientMaster_Select_Result> GetClientMaster_Select(string KeyWord, string CompanyGubun)
        {
            try
            {
                ObjectParameter Status = new ObjectParameter("iStatus", "varchar(3)");
                ObjectParameter Message = new ObjectParameter("iMessage", "varchar(255)");
                var q = ObjectContext.SP_ClientMaster_Select(KeyWord, CompanyGubun, Status, Message);
                return q.AsQueryable();
            }
            catch (Exception)
            { return null; }
        }

        public IEnumerable<SP_HardwareMaster_Select_Result> GetHardwareMaster_Select()
        {
            try
            {
                ObjectParameter Status = new ObjectParameter("iStatus", "varchar(3)");
                ObjectParameter Message = new ObjectParameter("iMessage", "varchar(255)");
                var q = ObjectContext.SP_HardwareMaster_Select(Status, Message);
                return q.AsEnumerable();
            }
            catch (Exception)
            { return null; }
        }

        //public SP_ClientMaster_Select_Result GetClientMaster_Select()
        //{
        //    return ObjectContext.get
        //}

        /// <summary>
        /// 업체정보 입력 로직
        /// </summary>
        /// <param name="CompanyID"></param>
        /// <param name="CompanyName"></param>
        /// <param name="Address"></param>
        /// <param name="Tel"></param>
        /// <param name="Date"></param>
        /// <param name="etc"></param>
        /// <returns></returns>
        public KMT_Pro.Web.ResultMessage ClientMaster_Insert(string CompanyID, string CompanyName, string Address, string Tel, string Date, string etc, string gubun)
        {
            try
            {
                ObjectParameter Status = new ObjectParameter("iStatus", "varchar(3)");
                ObjectParameter Message = new ObjectParameter("iMessage", "varchar(255)");

                this.ObjectContext.SP_ClientMaster_Insert(CompanyID, CompanyName, Address, Tel, Date, etc, gubun, Status, Message);

                KMT_Pro.Web.ResultMessage rsm = new KMT_Pro.Web.ResultMessage();
                if (Status.Value.Equals("OK"))
                {
                    rsm.IsSuccess = true;
                    rsm.ExceptionMessage = Message.Value.ToString();
                }
                else
                {
                    rsm.IsSuccess = false;
                    rsm.ExceptionMessage = Message.Value.ToString();
                }

                return rsm;
            }
            catch (Exception)
            { return null; }
        }

        /// <summary>
        /// 업체정보 삭제 로직
        /// </summary>
        /// <param name="msg"></param>
        /// <returns></returns>
        public KMT_Pro.Web.ResultMessage ClientMaster_Delete(string CompanyID)
        {
            try
            {
                ObjectParameter Status = new ObjectParameter("iStatus", "varchar(3)");
                ObjectParameter Message = new ObjectParameter("iMessage", "varchar(255)");
                var q = ObjectContext.SP_ClientMaster_Delete(CompanyID, Status, Message);
                KMT_Pro.Web.ResultMessage rsm = new KMT_Pro.Web.ResultMessage();
                if (Status.Value.Equals("OK"))
                {
                    rsm.IsSuccess = true;
                    rsm.ExceptionMessage = Message.Value.ToString();
                }
                else
                {
                    rsm.IsSuccess = false;
                    rsm.ExceptionMessage = Message.Value.ToString();
                }

                return rsm;
            }
            catch (Exception)
            { return null; }
        }

        /// <summary>
        /// 협력사 유저정보 조회 로직
        /// </summary>
        /// <param name="KeyWord"></param>
        /// <returns></returns>
        public IQueryable<SP_ClientUserMaster_Select_Result> GetClientUserMaster_Select(string KeyWord)
        {
            try
            {
                ObjectParameter Status = new ObjectParameter("iStatus", "varchar(3)");
                ObjectParameter Message = new ObjectParameter("iMessage", "varchar(255)");
                var q = ObjectContext.SP_ClientUserMaster_Select(KeyWord, Status, Message);
                return q.AsQueryable();
            }
            catch (Exception)
            { return null; }
        }

        /// <summary>
        /// 협력사 유저정보 삭제 로직
        /// </summary>
        /// <param name="Idx"></param>
        /// <returns></returns>
        public KMT_Pro.Web.ResultMessage ClientUserMaster_Delete(string Idx)
        {
            try
            {
                ObjectParameter Status = new ObjectParameter("iStatus", "varchar(3)");
                ObjectParameter Message = new ObjectParameter("iMessage", "varchar(255)");
                var q = ObjectContext.SP_ClientPerson_Delete(Idx, Status, Message);
                KMT_Pro.Web.ResultMessage rsm = new KMT_Pro.Web.ResultMessage();
                if (Status.Value.Equals("OK"))
                {
                    rsm.IsSuccess = true;
                    rsm.ExceptionMessage = Message.Value.ToString();
                }
                else
                {
                    rsm.IsSuccess = false;
                    rsm.ExceptionMessage = Message.Value.ToString();
                }

                return rsm;
            }
            catch (Exception)
            { return null; }
        }

        /// <summary>
        /// 협력사 유저정보용 콤보박스 로직
        /// </summary>
        /// <returns></returns>
        public List<ComboBoxCode> GetClientPerson_Combo()
        {
            try
            {
                ObjectParameter Status = new ObjectParameter("iStatus", "varchar(3)");
                ObjectParameter Message = new ObjectParameter("iMessage", "varchar(255)");
                var q = ObjectContext.SP_ClientPerson_Combo(Status, Message);
                return (from n in ObjectContext.SP_ClientPerson_Combo(Status, Message)
                        select new ComboBoxCode()
                        {
                            Name = n.CompanyID.Trim(),
                            Type = n.CompanyName.Trim()
                        }
                       ).ToList();
            }
            catch (Exception)
            { return null; }
        }

        /// <summary>
        /// 협력사 유저정보 입력 로직
        /// </summary>
        /// <param name="idx"></param>
        /// <param name="CompanyID"></param>
        /// <param name="Name"></param>
        /// <param name="Position"></param>
        /// <param name="Tel"></param>
        /// <param name="Phone"></param>
        /// <param name="Date"></param>
        /// <param name="etc"></param>
        /// <param name="IU_Gubun"></param>
        /// <returns></returns>
        public KMT_Pro.Web.ResultMessage ClientUserMaster_Insert(string idx, string CompanyID, string Name, string Position, string Tel, string Phone, string Fax, string Email, string Date, string etc, string IU_Gubun)
        {
            try
            {
                ObjectParameter Status = new ObjectParameter("iStatus", "varchar(3)");
                ObjectParameter Message = new ObjectParameter("iMessage", "varchar(255)");

                this.ObjectContext.SP_ClientUserMaster_Insert(Int32.Parse(idx.Trim()), CompanyID, Name, Position, Tel, Phone, Fax, Email, Date, etc, IU_Gubun, Status, Message);

                KMT_Pro.Web.ResultMessage rsm = new KMT_Pro.Web.ResultMessage();
                if (Status.Value.Equals("OK"))
                {
                    rsm.IsSuccess = true;
                    rsm.ExceptionMessage = Message.Value.ToString();
                }
                else
                {
                    rsm.IsSuccess = false;
                    rsm.ExceptionMessage = Message.Value.ToString();
                }

                return rsm;
            }
            catch (Exception)
            { return null; }
        }

        /// <summary>
        /// 하드웨어 정보 삭제 로직
        /// </summary>
        /// <param name="HardwareID"></param>
        /// <returns></returns>
        public KMT_Pro.Web.ResultMessage HardwareMaster_Delete(string HardwareID)
        {
            try
            {
                ObjectParameter Status = new ObjectParameter("iStatus", "varchar(3)");
                ObjectParameter Message = new ObjectParameter("iMessage", "varchar(255)");

                this.ObjectContext.SP_HardwareMaster_Delete(HardwareID, Status, Message);

                KMT_Pro.Web.ResultMessage rsm = new KMT_Pro.Web.ResultMessage();
                if (Status.Value.Equals("OK"))
                {
                    rsm.IsSuccess = true;
                    rsm.ExceptionMessage = Message.Value.ToString();
                }
                else
                {
                    rsm.IsSuccess = false;
                    rsm.ExceptionMessage = Message.Value.ToString();
                }

                return rsm;
            }
            catch (Exception)
            { return null; }
        }

        public KMT_Pro.Web.ResultMessage HardwareMaster_Insert(string HardwareID, string hardwareName, string hardwareType, string companyName, string companyTelNum, string aSlimit, string date, string etc, string Gubun)
        {
            try
            {
                ObjectParameter Status = new ObjectParameter("iStatus", "varchar(3)");
                ObjectParameter Message = new ObjectParameter("iMessage", "varchar(255)");

                this.ObjectContext.SP_HardwareMaster_Insert(HardwareID, hardwareName, hardwareType, companyName, companyTelNum, aSlimit, date, etc, Gubun, Status, Message);

                KMT_Pro.Web.ResultMessage rsm = new KMT_Pro.Web.ResultMessage();
                if (Status.Value.Equals("OK"))
                {
                    rsm.IsSuccess = true;
                    rsm.ExceptionMessage = Message.Value.ToString();
                }
                else
                {
                    rsm.IsSuccess = false;
                    rsm.ExceptionMessage = Message.Value.ToString();
                }

                return rsm;
            }
            catch (Exception)
            { return null; }
        }

        public KMT_Pro.Web.ResultMessage HardwareImage_Insert(string HardwareID, byte[] image, string IU)
        {
            try
            {
                ObjectParameter Status = new ObjectParameter("iStatus", "varchar(3)");
                ObjectParameter Message = new ObjectParameter("iMessage", "varchar(255)");

                this.ObjectContext.SP_HardwareImage_Insert(HardwareID, image, IU ,Status, Message);

                KMT_Pro.Web.ResultMessage rsm = new KMT_Pro.Web.ResultMessage();
                if (Status.Value.Equals("OK"))
                {
                    rsm.IsSuccess = true;
                    rsm.ExceptionMessage = Message.Value.ToString();
                }
                else
                {
                    rsm.IsSuccess = false;
                    rsm.ExceptionMessage = Message.Value.ToString();
                }

                return rsm;
            }
            catch (Exception)
            { return null; }
        }

        /// <summary>
        /// 유저 메뉴 세팅 로직
        /// </summary>
        /// <param name="KeyWord"></param>
        /// <returns></returns>
        public IQueryable<SP_UserMenuSelect_Result> GetUserMenuSelect(string Position)
        {
            try
            {
                ObjectParameter Status = new ObjectParameter("iStatus", "varchar(3)");
                ObjectParameter Message = new ObjectParameter("iMessage", "varchar(255)");
                var q = ObjectContext.SP_UserMenuSelect(Position, Status, Message);
                return q.AsQueryable();
            }
            catch (Exception)
            { return null; }
        }
        //-------------------------------------------------------------------------//
    }
}


